package com.woniuxy.yogaapp.handler;

import com.woniuxy.yogaapp.dto.Count;
import com.woniuxy.yogaapp.dto.UidDto;
import com.woniuxy.yogaapp.pojo.Coach;
import com.woniuxy.yogaapp.pojo.DetailOrder;
import com.woniuxy.yogaapp.pojo.OrderClass;
import com.woniuxy.yogaapp.pojo.Student;
import com.woniuxy.yogaapp.service.OrderShowService;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/*
 *@author：源
 *@date:14:15:26
 *@descripion
 */
@Data
@Controller
@RequestMapping("/ordershow")
public class OrderShowHandler {
	@Resource
	private OrderShowService orderShowService;

	/*
	 * 从前端传入uid，数据库获取sid相关所有订单相关的所有数据 流程： 1.通过uid查到sid，还是cid 2.通过
	 *
	 *
	 */
	@RequestMapping("showorder")
	@ResponseBody
	public List<OrderClass> selectOrder(UidDto uidDto) {
		// 待评论
		System.out.println(uidDto.getUid());
		List<OrderClass> list = orderShowService.selectOrder(uidDto);
		System.out.println("++++++++++");

		System.out.println("++++++++++");

		for (OrderClass orderClass : list) {

			System.out.println(orderClass);
		}
		return list;
	}


	@RequestMapping("showispayorder")
	@ResponseBody
	public List<OrderClass> selectIsPayOrder(UidDto uidDto) {
		// 待评论
		System.out.println(uidDto.getUid());
		List<OrderClass> list = orderShowService.selectOrder(uidDto);

		Iterator<OrderClass> iterator = list.iterator();
		while (iterator.hasNext()) {
			OrderClass orderClass = iterator.next();
			if (orderClass.getDetailOrders().isEmpty()) {
				iterator.remove();
			} else {
				Iterator<DetailOrder> iterator1 = orderClass.getDetailOrders().iterator();
				while (iterator1.hasNext()) {
					DetailOrder detailOrder = iterator1.next();
					if (detailOrder.getState() != 6) {
						iterator1.remove();
					}
				}
			}

		}
		Iterator<OrderClass> iterator3 = list.iterator();
		while (iterator3.hasNext()) {
			OrderClass orderClass = iterator3.next();
			if (orderClass.getDetailOrders().isEmpty()) {
				iterator3.remove();
			}
		}
		for (OrderClass orderClass : list) {

			System.out.println(orderClass);
		}
		return list;
	}

	@RequestMapping("showisappraiseorder")
	@ResponseBody
	public List<OrderClass> selectIsAppraiseOrder(UidDto uidDto) {
		// 待评论

		List<OrderClass> list = orderShowService.selectOrder(uidDto);

		Iterator<OrderClass> iterator = list.iterator();
		while (iterator.hasNext()) {
			OrderClass orderClass = iterator.next();
			if (orderClass.getDetailOrders().isEmpty()) {
				iterator.remove();
			} else {
				Iterator<DetailOrder> iterator1 = orderClass.getDetailOrders().iterator();
				while (iterator1.hasNext()) {
					DetailOrder detailOrder = iterator1.next();
					if (detailOrder.getState() != 4) {
						iterator1.remove();
					}
				}
			}

		}
		Iterator<OrderClass> iterator3 = list.iterator();
		while (iterator3.hasNext()) {
			OrderClass orderClass = iterator3.next();
			if (orderClass.getDetailOrders().isEmpty()) {
				iterator3.remove();
			}
		}
		for (OrderClass orderClass : list) {

			System.out.println(orderClass);
		}
		return list;
	}

	@RequestMapping("showissolveorder")
	@ResponseBody
	public List<OrderClass> selectIsSolveOrder(UidDto uidDto) {
		// 待评论

		List<OrderClass> list = orderShowService.selectOrder(uidDto);

		Iterator<OrderClass> iterator = list.iterator();
		while (iterator.hasNext()) {
			OrderClass orderClass = iterator.next();
			if (orderClass.getDetailOrders().isEmpty()) {
				iterator.remove();
			} else {
				Iterator<DetailOrder> iterator1 = orderClass.getDetailOrders().iterator();
				while (iterator1.hasNext()) {
					DetailOrder detailOrder = iterator1.next();
					if (detailOrder.getState() != 1) {
						iterator1.remove();
					}
				}
			}

		}
		Iterator<OrderClass> iterator3 = list.iterator();
		while (iterator3.hasNext()) {
			OrderClass orderClass = iterator3.next();
			if (orderClass.getDetailOrders().isEmpty()) {
				iterator3.remove();
			}
		}
		for (OrderClass orderClass : list) {

			System.out.println(orderClass);
		}
		return list;
	}

	// 统计未支付和未评论的值给前端
	@RequestMapping("count")
	@ResponseBody
	public Count selectCount(Student student) {
		// 获取sid通过uid

		Count count = orderShowService.selectCount(student);
		System.out.println(count);
		return count;
	}

	@RequestMapping("countc")
	@ResponseBody
	public Count selectCountc(Coach coach) {
		// 获取sid通过uid
		System.out.println(coach);
		Count count = orderShowService.selectCountc(coach);
		System.out.println(count);
		return count;
	}

}
